APM.MainFrameView = Ember.View.extend({

	didInsertElement: function(){
		var view = this,
			controller = view.get('controller'),
			$viewRoot = this.$(),
			$appConainer = $viewRoot.find(".application-content"),
			$appHeader = $viewRoot.find(".application-header "),		
			$mainContainer = $viewRoot.find(".application-main"),
			$leftToggle = $viewRoot.find(".application-btn-fold-left"),
			$w = $(window),
			leftlis = $viewRoot.find(".application-menu li");

		this.highlightMenu();
		leftlis.click(function(e){
			leftlis.removeClass("active");
			$(this).addClass("active");
		});

		this._setAppboxMinHeight();

		$leftToggle.click(function() {
			$appConainer.toggleClass("hideLeft");
			$appHeader.toggleClass("hideLeft");
			if($mainContainer[0].style.transition === undefined){
				//不支持css动画的直接发消息
				APM.trigger("mainContainerResize");
			}
			controller.set('hideLeft', $appConainer.hasClass('hideLeft'));

		});

		$mainContainer.on('transitionend', function(e) {
			if(!e.originalEvent){
				return;
			}
			var oriEvt = e.originalEvent,
				propertyName = oriEvt.propertyName;
			//避免各种css动画频繁触发该事件
			if(oriEvt.target != $mainContainer[0]
				&& (propertyName != "margin-left"||propertyName != "margin-top" )){
				return;
			}
			APM.trigger("mainContainerResize");
		});

		$(window).on("resize.mainframe", function(){
			view._setAppboxMinHeight();
		});

		controller.requireWargingNum();
	},

	willDestroyElement: function(){
		$(window).off("resize.mainframe");

		this.get("controller").stopQueryWargingNum();
	},

	_setAppboxMinHeight: function(){
		var $appbox = this.$().find(".application-main-box");
		var height = $(window).height()-49; // 为什么-68 有疑问 告警台有问题 这里
		$appbox.css("min-height", height+"px");
	},

	highlightMenu: function(){
		var hash = location.hash == "" ? "#" : location.hash.split("#")[1];
		var tarAppPath = decodeURIComponent(hash).split("/");
		var appname = tarAppPath[1];
		var firstPath = tarAppPath[2];

		this.$().find(".application-menu li")
			.removeClass("active")
			.filter(function(i, li){
				var liHash = $(li).find(">a").attr("href").split("#")[1]||"",
					liHashPath = liHash.split("/");
				if(appname == "business"){
					if(firstPath == "overview"){
						return "business" == liHashPath[1] && "overview" == liHashPath[2];
					}else{
						return "business" == liHashPath[1] && "overview" != liHashPath[2];
					}
				}else{
					return appname == liHashPath[1];
				}
			})
			.addClass("active");
	}
});